package org.hxyjs.service.impl;

import org.hxyjs.dao.PraiseResDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.PraiseRes;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.PraiseResService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("PraiseResService")
public class PraiseResServiceImpl implements PraiseResService {
    @Resource
    PraiseResDao praiseResDao;
    @Override
    public DTO<PraiseRes> getAllPraiseRes()  throws Exception{
        List<PraiseRes> praiseRess = praiseResDao.getAllPraiseRes();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(praiseRess).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<PraiseRes>(messageEnum.getCode(),messageEnum.getMessage(),praiseRess);
    }

    @Override
    public DTO<PraiseRes> deletePraiseResByUrid(String urid)  throws Exception{
        Integer result = praiseResDao.deletePraiseResByUrid(urid);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEADMINSUCCESS:MessageEnum.DELETEADMINFAIL;
        return new DTO<PraiseRes>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<PraiseRes> getPraiseResByUrid(String urid)  throws Exception{
        PraiseRes praiseRess = praiseResDao.getPraiseResByUrid(urid);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(praiseRess).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<PraiseRes>(messageEnum.getCode(),messageEnum.getMessage(),praiseRess);
    }

    @Override
    public DTO<PraiseRes> updatePraiseResByUrid(PraiseRes praiseRes) throws Exception {
        Integer result = praiseResDao.updatePraiseResByUrid(praiseRes);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEADMINSUCCESS:MessageEnum.UPDATEADMINFAIL;
        return new DTO<PraiseRes>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<PraiseRes> addPraiseRes(PraiseRes praiseRes) throws Exception {
        Integer result = praiseResDao.addPraiseRes(praiseRes);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.ADDADMINSUCCESS:MessageEnum.ADDADMINFAIL;
        return new DTO<PraiseRes>(messageEnum.getCode(),messageEnum.getMessage());
    }

    /**
     * 学生选课成功 学生成绩加1分，从选课信息中可以获得学生的学号就能查询出学生原来的成绩
     * @param
     * @return
     * @throws Exception
     */
    @Override
    public DTO<PraiseRes> getPraiseResByPage(PageInfo pageInfo) throws Exception {
        List<PraiseRes> praiseRess = praiseResDao.getPraiseResByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(praiseRess).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<PraiseRes>(messageEnum.getCode(),messageEnum.getMessage(),praiseRess,pageInfo);
    }

}
